package com.xxxy.jsq.action.type;

import com.alibaba.fastjson2.JSONObject;
import com.xxxy.jsq.dao.TypeDao;
import com.xxxy.jsq.model.Type;
import com.xxxy.jsq.model.Page;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/getAllTypesPageServlet")
public class getAllTypesPageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public getAllTypesPageServlet() {
        super();
    }
//
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Type model = new Type();
        TypeDao dao = new TypeDao();
        String type_id = request.getParameter("type_id");
        String type_name = request.getParameter("type_name");
        String type_count = request.getParameter("type_count");
        String type_flag = request.getParameter("type_flag");
        String type_years = request.getParameter("type_years");

        if (type_id != null && !type_id.equals("")) {
            model.setTypeId(Long.parseLong(type_id));
        }
        if (type_name != null && !type_name.equals("")) {
            model.setTypeName(type_name);
        }
        if (type_count != null && !type_count.equals("")) {
            model.setTypeCount(Integer.parseInt(type_count));
        }
        if (type_flag != null && !type_flag.equals("")) {
            model.setTypeFlag(type_flag);
        }
        if (type_years != null && !type_years.equals("")) {
            model.setTypeYears(type_years);
        }
        String cpage = request.getParameter("page");
        String limit = request.getParameter("limit");
        Page page = new Page();
        if (cpage != null && !cpage.equals("")) {
            page.setCurrentPage(Integer.parseInt(cpage));
            page.setCount(Integer.parseInt(limit));
        }
        List<Type> list = dao.getAllDataByPage(model, page);
        int total = dao.getCount(model);
        System.out.println(list);
        JSONObject jo = new JSONObject();
        jo.put("code", 0);
        jo.put("msg", "你好");
        jo.put("count", total);
        jo.put("data", list);
        System.out.println(jo.toJSONString());
        PrintWriter out = response.getWriter();
        out.print(jo.toString());
        out.flush();
        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}